<?php
namespace App\Logics;

use Illuminate\Http\Request;
use Illuminate\Support\Facades\DB;
use App\Models\BrandModel;
use App\Models\GoodsModel;

//逻辑处理
class BrandLogic extends BaseLogic {


    public function __construct()
    {
        parent::__construct();

    }

    public  function index(array $para){
        $page = isset($para['page'])?$para['page']:1;
        $per_page = isset($para['per_page'])?$para['per_page']:20;
        $brand_name = isset($para['brand_name'])?$para['brand_name']:'';
        $brand_id = isset($para['brand_id'])?$para['brand_id']:0;
        $brand_is_del = isset($para['brand_is_del'])?$para['brand_is_del']:-1;
        $is_official_auth = isset($para['is_official_auth'])?$para['is_official_auth']:-1;

        $where = $searchWhere = [];

        if ($brand_id > 0) {
            $where['brand_id'] = $brand_id;
            $searchWhere[] = ['brand_id', $brand_id];
        }
        if ($brand_is_del!=-1) {
            $where['brand_is_del'] = $brand_is_del;
            $searchWhere[] = ['brand_is_del', $brand_is_del];
        }
        if ($is_official_auth!=-1) {
            $where['is_official_auth'] = $is_official_auth;
            $searchWhere[] = ['is_official_auth', $is_official_auth];
        }

        if ($brand_name) {
            $where['brand_name'] = $brand_name;
            $searchWhere[] = ['brand_name', 'like', $brand_name . '%'];
        }
        if ($per_page != 20) {
            $where['per_page'] = $per_page;
        }



        $model = BrandModel::where($searchWhere);
        $data = $model->paginate($per_page, ['*'], 'page', $page);
        $data = ob_to_array($data);
        $data['page_list'] = $this->page($data['last_page'], $data['current_page']);
        $data['up_page'] = ($data['current_page'] - 1) > 0 ? $data['current_page'] - 1 : 1;
        $data['next_page'] = ($data['current_page'] + 1) > $data['last_page'] ? $data['last_page'] : $data['current_page'] + 1;
        $url = url()->current() . '?&';
        foreach ($where as $k => $v) {
            $url .= "$k=$v&";
        }
        $data['page_url'] = trim($url, '&');

        $url=config('sys.img_url').'/';
        $arr=[
            'data'=>$data,
            'where'=>$where,
            'url'=>$url
        ];
        return $arr;
    }

    public  function add(array $para,Request $request){
        $brand_name= isset($para['brand_name'])?$para['brand_name']:'';
        $brand_note=isset($para['brand_note'])?$para['brand_note']:'';
        $is_official_auth=isset($para['is_official_auth'])?$para['is_official_auth']:1;
        $is_official_auth==1?1:2;

        if(empty($brand_name)){
            return error('请填写品牌名称');
        }

        $brand_logo='';
        if($request->hasFile('img')){
            $data=$this->uploadImg($request,'img','brand');
            if($data['status']==0) return error($data['data']);
            $brand_logo=$data['data'];
        }
        $data=[
            'brand_name'=>$brand_name,
            'brand_note'=>$brand_note,
            'is_official_auth'=>$is_official_auth,
            'brand_logo'=>$brand_logo,
            'brand_is_del'=>1,
            'brand_creat_time'=>date("Y-m-d H:i:s")

        ];
        $res=BrandModel::insert($data);
        if($res){
            return success('添加成功');
        }
        if($data['cate_pic']) $this->deleteImg($data['cate_pic']);
        return error('添加失败');
    }

    public function edit(array $para,Request $request){
        $brand_name= isset($para['brand_name'])?$para['brand_name']:'';
        $brand_note=isset($para['brand_note'])?$para['brand_note']:'';
        $is_official_auth=isset($para['is_official_auth'])?$para['is_official_auth']:1;
        $brand_id=isset($para['brand_id'])?$para['brand_id']:0;
        $brand_is_del=isset($para['brand_is_del'])?$para['brand_is_del']:1;
        $is_official_auth==1?1:2;
        $brand_is_del==1?1:2;

        if(empty($brand_name)){
            return error('请填写品牌名称');
        }
        $brand=BrandLogic::getOne($brand_id,['brand_logo']);

        $brand_logo='';
        if($request->hasFile('img')){
            $data=$this->uploadImg($request,'img','brand');
            if($data['status']==0) return error($data['data']);
            $brand_logo=$data['data'];
        }
        $data=[
            'brand_name'=>$brand_name,
            'brand_note'=>$brand_note,
            'is_official_auth'=>$is_official_auth,
            'brand_is_del'=>$brand_is_del,
        ];
        if($brand_logo)$data['brand_logo']=$brand_logo;

        $res= BrandModel::where('brand_id',$brand_id)->update($data);
        if($res){
            if($brand_logo && $brand['brand_logo']) $this->deleteImg($brand['brand_logo']);
            return success('编辑成功');
        }
        if($brand_logo) $this->deleteImg($brand_logo);
        return error('编辑失败');
    }

    public  function delete(int $brand_id){
        $brand=BrandLogic::getOne($brand_id,['brand_logo']);
        if(empty($brand))  return error('参数有误');
        $num= GoodsModel::where('brand_id',$brand_id)->count();
        if($num>0)  return error('该品牌下有商品无法删除');
        $res= BrandModel::where('brand_id',$brand_id)->delete();
        if(!$res) return error('删除失败');
        if($brand['brand_logo']) $this->deleteImg($brand['brand_logo']);
         return success('删除成功');
    }

    public  function delImg(int $brand_id){
        $brand= BrandModel::where('brand_id',$brand_id)->first();
        if(empty($brand)) return error('参数错误');
        $brand=ob_to_array($brand);
        BrandModel::where('brand_id',$brand_id)->update(['brand_logo'=>'']);
        if($brand['brand_logo']) $this->deleteImg($brand['brand_logo']);
        return success('删除成功');
    }

    public static function ajaxFieldsEdit(array $para){
        $brand_id = isset($para['brand_id'])?$para['brand_id']:0;
        $type = isset($para['type'])?$para['type']:'';

        if(empty($brand_id))  return error('参数错误');
        if(!in_array($type,['brand_is_del','is_official_auth']))  return error('参数错误');
        $brand= BrandLogic::getOne($brand_id,['brand_is_del','is_official_auth']);

        $res='';
        $zhi=0;
        $color='red';
        if($type=='brand_is_del'){
            $brand_is_del=$brand['brand_is_del']==1?2:1;
            $zhi=$brand_is_del;
            $color= $zhi==1?'blue':'red';
            $res= BrandModel::where('brand_id',$brand_id)->update(['brand_is_del'=>$brand_is_del]);
        }

        if($type=='is_official_auth'){
            $is_official_auth=$brand['is_official_auth']==1?2:1;
            $zhi=$is_official_auth;
            $color=$zhi==2?'blue':'red';
            $res= BrandModel::where('brand_id',$brand_id)->update(['is_official_auth'=>$is_official_auth]);
        }
        if($res)  return success('ok',['zhi'=>$zhi,'color'=>$color]);
         return error('编辑失败');
    }


    public static function getBrand($fields=['brand_id','brand_name']){
        $where=[
            ['cha_id',0],
            ['brand_is_del',1],
            //['is_official_auth',2]
        ];
        $data= BrandModel::where($where)->select($fields)->get();
        return ob_to_array($data);
    }

    //获取一个
    public static function  getOne($brand_id,$fields=['*']){
        $brand_id=(int)$brand_id;
        $data= BrandModel::where('brand_id',$brand_id)->select($fields)->first();
        return ob_to_array($data);
    }


    //判断该数据是否存在
    public static function isOne($brand_id){
        $brand_id=(int)$brand_id;

        return BrandModel::where('brand_id',$brand_id)->count();
    }





}
